//
//  HMSQLiteTool.swift
//  webo
//
//  Created by apple on 2016/11/26.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import FMDB
class HMSQLiteTool: NSObject {

    static let shared = HMSQLiteTool()
    //创建数据库文件 并l且打开数据库连接
    //单例对象一旦创建的时候就打开数据
    //必须有值
    let queue:FMDatabaseQueue
    
    override init() {
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("weibo.db")
        queue = FMDatabaseQueue(path: path)
        super.init()
        print("数据库路劲\(path)")
        createTable()
    }
    
    private func createTable(){
        
        let sql = "CREATE TABLE IF NOT EXISTS T_Status (statusId INTEGER PRIMARY KEY NOT NULL, status TEXT, userId TEXT, create_date TEXT DEFAULT (datetime('now','localtime')))"
        
        //使用queue中数据库操作的核心对象来执行sql语句
        queue.inTransaction { (db, rollback) in
            //rollback 如果操作失败就执行回滚的操作
            let istrue = db!.executeStatements(sql)
            if istrue {
                print("建标成功")
            }else{
                print("建表失败")
                rollback?.pointee = true
            }
        }
        
    }
    
    
}
